package com.zxmr.hospital.info.action;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.account.Role;
import com.zxmr.domain.account.User;
import com.zxmr.domain.service.AuthorizationService;
import com.zxmr.hospital.info.action.dto.JSONListDTO;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.UserTraceManager;

public class RoleManage extends Executor{

	private enum Method{
		queryAll,
		delete,
		add,
		update;
	}
	@Override
	public void execute(Request request, Response response) {
		
		Method method = Enum.valueOf(Method.class, this.getElement().getMethod());
		try
		{
			switch(method)
			{
			case queryAll:
				query(request,response);
				break;
			case delete:
				delete(request,response);
				break;
			case add:
				add(request,response);
				break;
			case update:
				update(request,response);
				break;
			}
		}catch(ExecuteException e)
		{
			
		}

	}

	private void query(Request request, Response response)throws ExecuteException
	{
		User user = (User)request.getAccount();
		AuthorizationService service = new AuthorizationService();
		List<Role> roles = service.queryAllRole(user.getCompany());
		
		JSONListDTO dto = new JSONListDTO();
		dto.setRows(roles);
		
		response.setPayload(new ResponsePayload(JSON.toJSONString(dto)));
	}
	private void delete(Request request, Response response)throws ExecuteException
	{
		Role dto = new Role();
		AuthorizationService service = new AuthorizationService();
		setRoleID(request.getRequestParameters().get("id"),dto);
		User user = (User)request.getAccount();
		
		authRole(user,dto.getId());
		
		if(service.deleteRole(dto))
		{
			response.setPayload(new ResponsePayload("success"));
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.deleteRole",dto.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}else
		{
			response.setResult(ResultCode.FAILED);
		}
	}
	private void add(Request request, Response response)throws ExecuteException
	{
		User user = (User)request.getAccount();
		Role dto = new Role();
		setRoleName(request.getRequestParameters().get("name"),dto);

		dto.setCompany(user.getCompany());
		AuthorizationService service = new AuthorizationService();
		if(service.addRole(dto))
		{
			response.setPayload(new ResponsePayload("success"));
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.addRole",dto.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}else
		{
			response.setResult(ResultCode.FAILED);
		}

	}
	private void update(Request request, Response response)throws ExecuteException
	{
		Role dto = paraToRole(request);
		
		AuthorizationService service = new AuthorizationService();
		User user = (User)request.getAccount();
		
		authRole(user,dto.getId());
		dto.setCompany(user.getCompany());
		
		if(service.updateRole(dto))
		{
			response.setPayload(new ResponsePayload("success"));
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.updateRole",dto.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}else
		{
			response.setResult(ResultCode.FAILED);
		}
	}
	private void authRole(User user,int role)throws ExecuteException
	{
		AuthorizationService service = new AuthorizationService();
		Role old = service.queryRole(role);
		
		if(old == null)throw new ExecuteException("can not find role");
		if(user.getCompany() != old.getCompany())throw new ExecuteException("you are not authorized");
	}
	private Role paraToRole(Request request)throws ExecuteException
	{
		Role dto = new Role();
		setRoleID(request.getRequestParameters().get("id"),dto);
		setRoleName(request.getRequestParameters().get("name"),dto);
		return dto;
	}
	private void setRoleID(String id,Role role)throws ExecuteException
	{
		if(id!=null && !"".equals(id.trim()))
		{
			try
			{
				role.setId(Integer.parseInt(id));
			}catch(Exception e)
			{
				throw new ExecuteException("id is not a numeric");
			}
		}
	}
	private void setRoleName(String name,Role role)throws ExecuteException
	{
		if(name == null || "".equals(name.trim()))
		{
			throw new ExecuteException("name can not be empty");
		}
		role.setName(name);
	}
}
